﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
using senController.classes;

namespace senController.Classes
{
    class ControllerTimer
    {
        System.Timers.Timer timer;
        private int _startTime, _currentTime;
        private int _tickTime;
        private Controller _controller;

        public ControllerTimer(Controller controller, int tickTime, int startTime = 0)
        {
            this._startTime = startTime;
            this._controller = controller;
            timer = new System.Timers.Timer(tickTime);
            this._tickTime = tickTime;
            this._currentTime = startTime;
        }

        /// <summary>
        /// Start timer
        /// </summary>
        public void run()
        {
            this.timer.Start();
            this._controller.Message("Timer gestart");
            this.timer.Elapsed += new ElapsedEventHandler(TimerTick);
        }

        public void stop()
        {
            this.timer.Stop();
        }

        /// <summary>
        /// Run script every timer tick
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TimerTick(object sender, ElapsedEventArgs e)
        {
            _currentTime += _tickTime;
            _controller.TrafficControl(_tickTime);
            _controller.TrafficLightControl(_currentTime);
            TimeSpan timeSpan = TimeSpan.FromMilliseconds(_currentTime);
            _controller.TimeUpdate(((int)timeSpan.Hours).ToString().PadLeft(2,'0') + ":" + ((int)timeSpan.Minutes).ToString().PadLeft(2,'0') + ":" + ((int)timeSpan.Seconds).ToString().PadLeft(2,'0'));
        }
    }
}
